package com.woniuxy.store.app.apiImpl;

import com.woniuxy.cloud.mall95.log.Log;
import com.woniuxy.store.app.util.WebUtil;
import com.woniuxy.store.client.StorePayManageClient;
import com.woniuxy.store.infra.model.StoreContract;
import com.woniuxy.store.service.StoreContractService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import result.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@RequestMapping("/pay")
@RestController
public class StorePayManageClientImpl implements StorePayManageClient {

    @Resource
    private StoreContractService storeContractService;



    @Override
    @Log("开始支付套餐费用")
    public void payContract( String contractNum) throws Exception {
        String result = storeContractService.payContract(contractNum);
        HttpServletResponse response = WebUtil.getResponse();
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf8");
        response.getWriter().write(result);
        response.flushBuffer();
    }

    @Override
    public Result payCallback() throws Exception {
        String trade_status = WebUtil.getRequest().getParameter("trade_status");
        if (trade_status.equals("TRADE_SUCCESS")){
            String orderId = WebUtil.getRequest().getParameter("out_trade_no");
            StoreContract contract = storeContractService.getById(Integer.valueOf(orderId));
            storeContractService.payCallback(contract);
        }
        return Result.success();
    }
}
